package cn.wolfcode.luowowo.community.service.impl;

import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.community.mapper.QuestionMapper;
import cn.wolfcode.luowowo.community.query.QuestionQuery;
import cn.wolfcode.luowowo.community.service.IQuestionService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private IQuestionService questionService;

    public PageInfo query(QuestionQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());

        return new PageInfo(questionMapper.selectForList(qo));
    }


    public Question selectQuestionById(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    public Question get(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(Question question) {
        if(question.getId()==null){
            //创建时间
            question.setCreatetime(new Date());
            questionMapper.insert(question);
        }
        questionMapper.updateByPrimaryKey(question);
    }

    @Override
    public List<Question> selectAll() {
        return questionMapper.selectAll();
    }

    @Override
    public void updateNum(Long answerId) {
        Question question = questionService.get(answerId);
        question.setThumbsupnum(question.getThumbsupnum()+1);
        questionMapper.updateForNum(question);
    }

    @Override
    public PageInfo queryForNum(QuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"q.thumbsupnum desc");
        return new PageInfo(questionMapper.selectForList(qo));
    }


}
